//
//  CLReadMenu.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/7/22.
//

#import "CLReadMenu.h"
#import "CLReadController.h"
#import "CLReadContentView.h"
#import "CLRMSettingView.h"
#import "CLReadLongPressView.h"
@interface CLReadMenu()
/// 点击这些控件不需要执行手势
@property (nonatomic, strong) NSArray *classStrings;
/// 动画是否完成
@property (nonatomic, assign) BOOL isAnimateComplete;

@end

@implementation CLReadMenu

- (instancetype)initWithVc:(CLReadController *)vc delegate:(id<CLReadMenuDelegate>)delegate {
    self = [super init];
    if (self) {
        // 记录
        _isAnimateComplete = YES;
        self.vc = vc;
        self.contentView = vc.contentView;
        self.delegate = delegate;
        self.classStrings = @[@"CLRMTopView",@"CLRMBottomView",@"CLRMSettingView",@"CLRMFontSizeView", @"CLRMFontTypeView",@"CLRMLightView",@"CLRMSpacingView",@"CLRMEffectTypeView",@"CLRMBGColorView",@"CLRMFuncView",@"CLRMProgressView",@"UIControl",@"UISlider",@"ASValueTrackingSlider",@"_UIPageViewControllerContentView"];
        // 隐藏状态栏
        [[UIApplication sharedApplication] setStatusBarHidden:!self.isMenuShow withAnimation:UIStatusBarAnimationFade];
        
        // 允许获取电量信息
        [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
        
        // 隐藏导航栏
        vc.viewModel.prefersNavigationBarHidden = YES;
        vc.fd_prefersNavigationBarHidden = YES;
        
        // 禁止手势返回
        vc.fd_interactivePopDisabled = YES;
        
        // 添加单机手势
        [self initTapGestureRecognizer];
        
        // 初始化日夜间遮盖
        [self initCover];
        
        // 初始化TopView
        [self initTopView];
        
        // 初始化SettingView
        [self initSettingView];
        
        // 初始化BottomView
        [self initBottomView];
    }
    return self;
}

// MARK: -- 添加单机手势

/// 添加单机手势
- (void)initTapGestureRecognizer {
    
    // 单击手势
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchSingleTap)];//UITapGestureRecognizer(target: self, action: #selector(touchSingleTap))
    _singleTap = singleTap;
    singleTap.numberOfTapsRequired = 1;
    singleTap.delegate = self;
    [self.vc.contentView addGestureRecognizer:singleTap];
}

// 触发单击手势
- (void)touchSingleTap {
    
    [self showMenu:!self.isMenuShow];
    
}

// MARK: -- UIGestureRecognizerDelegate

/// 手势拦截
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    NSString *classString = NSStringFromClass([touch.view class]);//String(describing: type(of: touch.view!))
    NSLog(@"classString:%@",classString);
    if ([self.classStrings containsObject:classString]) {
        
        return NO;
    }
    
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([gestureRecognizer isEqual:self.singleTap] && [otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        CGPoint touchPoint = [self.singleTap locationInView:self.vc.contentView];
        CGRect rect = CGRectInset(self.vc.contentView.frame, self.vc.contentView.width * 0.25, self.vc.contentView.height * 0.25);
        if (CGRectContainsPoint(rect, touchPoint)) {
            NSLog(@"g:%@,o:%@",gestureRecognizer,otherGestureRecognizer);
            return YES;
        }
    }
    return NO;
}

// MARK: 日夜间遮盖

/// 初始化日夜间遮盖
- (void)initCover {
    
    UIView *cover = [[UIView alloc]init];
    self.cover = cover;
    cover.alpha = (CGFloat)[CLUserDefaults boolForKey:CL_READ_KEY_MODE_DAY_NIGHT];
    cover.userInteractionEnabled = NO;
    cover.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.7];
    [self.vc.view addSubview:cover];
    cover.frame = self.vc.view.bounds;
}

// MARK: -- TopView

/// 初始化TopView
- (void)initTopView {
    
    CLRMTopView *topView = [[CLRMTopView alloc]initWithReadMenu:self];
    
    self.topView = topView;
    
    topView.hidden = !_isMenuShow;
    
    [self.contentView addSubview:topView];
    
    CGFloat y = _isMenuShow ? 0 : -CL_READ_MENU_TOP_VIEW_HEIGHT;
    
    topView.frame = CGRectMake(0, y, CL_READ_CONTENT_VIEW_WIDTH, CL_READ_MENU_TOP_VIEW_HEIGHT);
}

// MARK: -- BottomView

/// 初始化BottomView
- (void)initBottomView {
    
    CLRMBottomView *bottomView = [[CLRMBottomView alloc]initWithReadMenu:self];
    self.bottomView = bottomView;

    bottomView.hidden = !_isMenuShow;
    
    [self.contentView addSubview:bottomView];
    
    CGFloat y = _isMenuShow ? (CL_READ_CONTENT_VIEW_HEIGHT - CL_READ_MENU_BOTTOM_VIEW_HEIGHT) : CL_READ_CONTENT_VIEW_HEIGHT;
    
    bottomView.frame = CGRectMake(0, y, CL_READ_CONTENT_VIEW_WIDTH, CL_READ_MENU_BOTTOM_VIEW_HEIGHT);
    
    
    // 绘制中间虚线(如果不需要虚线可以去掉自己加个分割线)
    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc]init];
    
    shapeLayer.bounds = bottomView.bounds;
    
    shapeLayer.position = CGPointMake(bottomView.frame.size.width / 2, bottomView.frame.size.height / 2);
    
    shapeLayer.fillColor = UIColor.clearColor.CGColor;
    
    shapeLayer.strokeColor = CL_READ_COLOR_MENU_COLOR.CGColor;
    
    shapeLayer.lineWidth = CL_SPACE_LINE;
    
    shapeLayer.lineJoin = kCALineJoinRound;
    
    shapeLayer.lineDashPhase = 0;
    
    shapeLayer.lineDashPattern = @[@(1), @(2)];
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPoint point = CGPointMake(0, CL_READ_MENU_PROGRESS_VIEW_HEIGHT);
    CGPathMoveToPoint(path, NULL, point.x, point.y);
    
    CGPathAddLineToPoint(path, NULL, bottomView.frame.size.width, CL_READ_MENU_PROGRESS_VIEW_HEIGHT);
    
    shapeLayer.path = path;
    
    [bottomView.layer addSublayer:shapeLayer];
}

// MARK: -- SettingView

/// 初始化SettingView
- (void)initSettingView {
    
    CLRMSettingView *settingView = [[CLRMSettingView alloc]initWithReadMenu:self];
    
    self.settingView = settingView;
    
    settingView.hidden = YES;
    
    [self.contentView addSubview:settingView];
    CGFloat height = CL_READ_MENU_SETTING_VIEW_TOTAL_HEIGHT;
    settingView.frame = CGRectMake(0, CL_READ_CONTENT_VIEW_HEIGHT, CL_READ_CONTENT_VIEW_WIDTH, height);
    
}

// MARK: 菜单展示



- (void)showMenu:(BOOL)isShow {
    WEAK_SELF;
    if (_isMenuShow == isShow || !_isAnimateComplete) {return;}
    
    _isAnimateComplete = NO;
    
    if (isShow) {
        if ([self.delegate respondsToSelector:@selector(readMenuWillDisplay:)]) {
            [self.delegate readMenuWillDisplay:self];
        }
    } else {
        if ([self.delegate respondsToSelector:@selector(readMenuWillEndDisplay:)]) {
            [self.delegate readMenuWillEndDisplay:self];
        }
    }
    
    _isMenuShow = isShow;
    
    [self showBottomView:isShow completion:nil];

    [self showSettingView:NO completion:nil];
    
    [self showTopView:isShow completion:^{
        weakSelf.isAnimateComplete = YES;
        if (isShow) {
            if ([self.delegate respondsToSelector:@selector(readMenuDidDisplay:)]) {
                [self.delegate readMenuDidDisplay:self];
            }
        } else {
            if ([self.delegate respondsToSelector:@selector(readMenuDidEndDisplay:)]) {
                [self.delegate readMenuDidEndDisplay:self];
            }
        }
    }];
}

/// TopView展示
- (void)showTopView:(BOOL)isShow completion:(VoidBlock _Nullable)completion {
    
    [UIApplication.sharedApplication setStatusBarHidden:!isShow withAnimation:UIStatusBarAnimationSlide];
    
    if (isShow) { self.topView.hidden = NO; }
    
    [UIView animateWithDuration:CL_READ_AD_TIME delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        CGFloat y = isShow ? 0 : -CL_READ_MENU_TOP_VIEW_HEIGHT;
        CGRect frame = self.topView.frame;
        frame.origin = CGPointMake(0, y);
        self.topView.frame = frame;
    } completion:^(BOOL finished) {
        if (!isShow) { self.topView.hidden = YES; }
        CLSafeExecuteBlock(completion);
    }];
}

/// BottomView展示
- (void)showBottomView:(BOOL)isShow completion:(VoidBlock _Nullable)completion {

    if (isShow) { self.bottomView.hidden = false; }
    [UIView animateWithDuration:CL_READ_AD_TIME animations:^{
        CGFloat y = isShow ? (CL_READ_CONTENT_VIEW_HEIGHT - CL_READ_MENU_BOTTOM_VIEW_HEIGHT) : CL_READ_CONTENT_VIEW_HEIGHT;
        CGRect frame = self.bottomView.frame;
        frame.origin = CGPointMake(0, y);
        self.bottomView.frame = frame;
    } completion:^(BOOL finished) {
        if (!isShow) { self.bottomView.hidden = YES; }
        CLSafeExecuteBlock(completion);
    }];
}

/// SettingView展示
- (void)showSettingView:(BOOL)isShow completion:(VoidBlock _Nullable)completion {
  
    if (isShow) { self.settingView.hidden = NO; }
    
    [UIView animateWithDuration:CL_READ_AD_TIME delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        CGFloat y = isShow ? (CL_READ_CONTENT_VIEW_HEIGHT - CL_READ_MENU_SETTING_VIEW_TOTAL_HEIGHT) : CL_READ_CONTENT_VIEW_HEIGHT;
        CGRect frame = self.settingView.frame;
        frame.origin = CGPointMake(0, y);
        self.settingView.frame = frame;
        
    } completion:^(BOOL finished) {
        if (!isShow) { self.settingView.hidden = YES; }
        CLSafeExecuteBlock(completion);
    }];
}

@end
